package com.ruoyi.system.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.WGood;
import com.ruoyi.system.domain.WGoodsRecords;
import com.ruoyi.system.domain.vo.WGoodsRecordsVO;
import com.ruoyi.system.mapper.WGoodMapper;
import com.ruoyi.system.mapper.WGoodsRecordsMapper;
import com.ruoyi.system.service.IWGoodsRecordsService;

/**
 * 物品库存记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-05
 */
@Service
public class WGoodsRecordsServiceImpl implements IWGoodsRecordsService 
{
    @Autowired
    private WGoodsRecordsMapper wGoodsRecordsMapper;
    
    @Autowired
    private WGoodMapper wGoodMapper;

    
    @Transactional
    @Override
    public R check(List<WGoodsRecords> wGoodsRecordsList) {
    	for (WGoodsRecords wGoodsRecords : wGoodsRecordsList) {
    		Long goodsId = wGoodsRecords.getGoodsId();
    		String createBy = wGoodsRecords.getCreateBy();
    		if(goodsId == null ) return R.error("没传物品id");
    		WGood selectWGoodById = wGoodMapper.selectWGoodById(goodsId);
    		Integer changeNumber = wGoodsRecords.getChangeNumber();
    		if(changeNumber == null || changeNumber < 0 ) return R.error("没传数量");
    		
    		BeanUtils.copyBeanProp(wGoodsRecords, selectWGoodById);
    		wGoodsRecords.setCustomerId(selectWGoodById.getCustomerId());
    		wGoodsRecords.setDeptId(selectWGoodById.getDeptId());
    		wGoodsRecords.setGoodsName(selectWGoodById.getGoodsName());
    		wGoodsRecords.setIsIn(2);
    		wGoodsRecords.setCreateBy(createBy);
    		wGoodsRecords.setGoodsId(goodsId);
    		wGoodsRecords.setChangeNumber(changeNumber);
		}
    	
    	for (WGoodsRecords wGoodsRecords : wGoodsRecordsList) {
    		WGood wGood = new WGood();
    		wGood.setId(wGoodsRecords.getGoodsId());
    		wGood.setNumber(wGoodsRecords.getChangeNumber().longValue());
			wGoodMapper.updateWGood(wGood );
			wGoodsRecordsMapper.insertWGoodsRecords(wGoodsRecords);
    	}
    	return R.success();
    }
    
    /**
     * 查询物品库存记录
     * 
     * @param id 物品库存记录主键
     * @return 物品库存记录
     */
    @Override
    public WGoodsRecords selectWGoodsRecordsById(Long id)
    {
        return wGoodsRecordsMapper.selectWGoodsRecordsById(id);
    }

    /**
     * 查询物品库存记录列表
     * 
     * @param wGoodsRecords 物品库存记录
     * @return 物品库存记录
     */
    @Override
    public List<WGoodsRecords> selectWGoodsRecordsList(WGoodsRecords wGoodsRecords)
    {
        return wGoodsRecordsMapper.selectWGoodsRecordsList(wGoodsRecords);
    }

    @Override
    public List<WGoodsRecordsVO> selectWGoodsRecordsListNew(WGoodsRecordsVO WGoodsRecordsVO) {
    	 return wGoodsRecordsMapper.selectWGoodsRecordsListNew(WGoodsRecordsVO);
    }
    
    /**
     * 新增物品库存记录
     * 
     * @param wGoodsRecords 物品库存记录
     * @return 结果
     */
    @Override
    public int insertWGoodsRecords(WGoodsRecords wGoodsRecords)
    {
        wGoodsRecords.setCreateTime(DateUtils.getNowDate());
        return wGoodsRecordsMapper.insertWGoodsRecords(wGoodsRecords);
    }

    /**
     * 修改物品库存记录
     * 
     * @param wGoodsRecords 物品库存记录
     * @return 结果
     */
    @Override
    public int updateWGoodsRecords(WGoodsRecords wGoodsRecords)
    {
        wGoodsRecords.setUpdateTime(DateUtils.getNowDate());
        return wGoodsRecordsMapper.updateWGoodsRecords(wGoodsRecords);
    }

    /**
     * 批量删除物品库存记录
     * 
     * @param ids 需要删除的物品库存记录主键
     * @return 结果
     */
    @Override
    public int deleteWGoodsRecordsByIds(Long[] ids)
    {
        return wGoodsRecordsMapper.deleteWGoodsRecordsByIds(ids);
    }

    /**
     * 删除物品库存记录信息
     * 
     * @param id 物品库存记录主键
     * @return 结果
     */
    @Override
    public int deleteWGoodsRecordsById(Long id)
    {
        return wGoodsRecordsMapper.deleteWGoodsRecordsById(id);
    }
}
